/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.notaria.guerrasalas.be;

import com.notaria.guerrasalas.util.UtilGS;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

/**
 *
 * @author Administrador
 */
@Entity
@Table(name = "FACTURA", catalog = "BASE_NOTARIAL", schema = "dbo")
@NamedQueries({
    @NamedQuery(name = "Factura.findAll", query = "SELECT f FROM Factura f"),
    @NamedQuery(name = "Factura.findByNumero", query = "SELECT f FROM Factura f WHERE f.numero = :numero"),
    @NamedQuery(name = "Factura.findByIdFactura", query = "SELECT f FROM Factura f WHERE f.idDocumentoVenta = :idFactura"),
    @NamedQuery(name = "Factura.findByCliente", query = "SELECT f FROM Factura f WHERE UPPER(f.cliente.nombres) Like :nombres or UPPER(f.cliente.apellidoPaterno) Like :apPaterno or UPPER(f.cliente.apellidoMaterno) Like :apMaterno"),
    @NamedQuery(name = "Factura.findBetweenDates", query = "SELECT f FROM Factura f WHERE f.fechaCreacion BETWEEN :fechaInicio AND :fechaFin ORDER BY f.idDocumentoVenta DESC")
})
@PrimaryKeyJoinColumn(name = "ID_FACTURA", referencedColumnName = "ID_DOCUMENTO_VENTA")
@DiscriminatorValue(value = "FC")
public class Factura
        extends DocumentoVenta
        implements Serializable {

    @Basic(optional = false)
    @Column(name = "NUMERO", nullable = false, length = 10)
    private String numero;
    @Basic(optional = false)
    @Column(name = "SUB_TOTAL", nullable = false, precision = 19, scale = 4)
    private Double subTotal;
    @Basic(optional = false)
    @Column(name = "IGV", nullable = false, precision = 19, scale = 4)
    private Double igvCalculado;
    @JoinColumn(name = "FID_IGV", referencedColumnName = "ID_IGV", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Igv igv;
    @JoinColumn(name = "FID_USUARIO", referencedColumnName = "ID_USUARIO", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Usuario usuario;

    public Factura() {
    }

    public Factura(Long idDocumentoVenta) {
        super(idDocumentoVenta);
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }

    public Double getSubTotal() {
        return subTotal;
    }

    public void setSubTotal(Double subTotal) {
        this.subTotal = subTotal;
    }

    public Igv getIgv() {
        return igv;
    }

    public void setIgv(Igv igv) {
        this.igv = igv;
    }

    public Double getIgvCalculado() {
        return igvCalculado;
    }

    public void setIgvCalculado(Double igvCalculado) {
        this.igvCalculado = igvCalculado;
    }

    public String getNroFacturaConFormato(){
        return UtilGS.agregarCerosIzquierda(getNumero(), 7);
    }

    public void calcularIGVYSubtotal(){
        setIgvCalculado(getTotal()*igv.getValor());
        setSubTotal(getTotal()-getIgvCalculado());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DocumentoVenta other = (DocumentoVenta) obj;
        if (this.idDocumentoVenta != other.idDocumentoVenta && (this.idDocumentoVenta == null || !this.idDocumentoVenta.equals(other.idDocumentoVenta))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 59 * hash + (this.idDocumentoVenta != null ? this.idDocumentoVenta.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return idDocumentoVenta.toString();
    }
}
